package com.atguigu.gmall.serviceutil.service.impl;

import com.atguigu.gmall.serviceutil.properties.MinioProperties;
import com.atguigu.gmall.serviceutil.service.OssService;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2025/10/22 3:55
 */
@Service
public class OssServiceImpl implements OssService {
    @Autowired
    private MinioProperties minioProperties;
    @Autowired
    private MinioClient minioClient;
    @Override
    public String fileUpload(MultipartFile file) throws Exception {
        //获取文件名
        String fileName =UUID.randomUUID().toString().replaceAll("-","")+"_"+file.getOriginalFilename();
        //-1 代表不分块上传
        PutObjectOptions putObjectOptions = new PutObjectOptions(file.getSize(), -1);
        putObjectOptions.setContentType(file.getContentType());
        minioClient.putObject(minioProperties.getBucketName(),fileName,file.getInputStream(),putObjectOptions);
        return minioProperties.getEndpoint()+"/"+minioProperties.getBucketName()+"/"+fileName;
    }
}
